package handle;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import model.ShareFileModel;
import model.client.LocalClientModel;
import model.command.CommandLogout;
import model.command.ConnectComand;
import model.command.SendFileCommand;
import model.queue.LocalFileUpNotifyQueue;
import operate.LocalServerOperate;
import util.LogServerUtil;
import utils.CloseUtil;

import common.ClientModel;
import common.ClientHandle;
import common.Command;
import common.Server;

/**
 * 处理来自client的请求.
 *
 * @author guanweichen
 * @version Jan 18, 2011
 */
public class LocalClientCommandHandle extends ClientHandle {

	public LocalClientCommandHandle(Server server, Socket client) {
		this.server = server;
		this.client = client;
	}
	@Override
	public void run() {
		
		if (server.isStarted()) {
			Command command = null;
			try {
				System.out.println("Client " + client.getLocalSocketAddress().toString() + " send a command.............." );
				ObjectInputStream cmdInput = new ObjectInputStream(client.getInputStream());
				command = (Command) cmdInput.readObject() ;
			} catch ( IOException e) {
				System.out.println("ERROR: read client message error..........................");
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				System.out.println("ERROR: read client message error..........................");
				e.printStackTrace();
			}
			System.out.println("Server execute command: " + command.getCommand() );
			LocalServerOperate operate = new LocalServerOperate(this.server, client);
			if (Command.CONNECT.equals(command.getCommand())) {// 第一次Client连接
				
//				System.out.println("CLIENT : " + client.getLocalSocketAddress());
//				System.out.println("CLIENT : " + client.getLocalAddress());
//				System.out.println("CLIENT : " + client.getRemoteSocketAddress());
//				System.out.println("CLIENT : " + client.getInetAddress());
				ConnectComand comand = (ConnectComand) command;
				if( LogServerUtil.isRegisterAddress(client.getInetAddress().toString().replace("/", "")) ){// 是否是受限制IP
					operate.connect(client, comand);
				} else {
					operate.limitIpMessage(client);
					System.out.println("CLIENT : " + client.getInetAddress() + "is limited ip.");
					return;
				}
			} else if (Command.GET_FILE.equals(command.getCommand())) {// 请求文件
				SendFileCommand sendFileCommand = (SendFileCommand) command;
				operate.sendLogFile(client, sendFileCommand.getFileName(), sendFileCommand);// 发送文件to client
				// 移出旧的文件更新监听,加入刚刚请求的文件更新监听
				server.removeFileUpNotifyByAddressAndPort(client.getInetAddress().toString(), sendFileCommand.getPort());
				ShareFileModel shareFile = new ShareFileModel();
				for (ShareFileModel file : server.getListShareFile()) {
					if (file.getFileName().equals(sendFileCommand.getFileName())) {
						shareFile.setEncoding(file.getEncoding());
						shareFile.setFileName(file.getFileName());
						shareFile.setServerFilePath(file.getServerFilePath());
					}
				}
				LocalClientModel localClient = new LocalClientModel(client.getInetAddress().toString(), sendFileCommand.getPort());
				LocalFileUpNotifyQueue notifyQueue = new LocalFileUpNotifyQueue(localClient, shareFile);
				server.addFileUpNotify(notifyQueue);
			} else if (Command.GET_CLIENT_SHARE_FILES.equals(command.getCommand())) {// 请求最新共享文件列表
				operate.sendListShareFile(client);
			} else if (Command.EXIT.equals(command.getCommand())) {// 请求最新共享文件列表
				CommandLogout logout = (CommandLogout) command;
				operate.clientLogout(client, logout);
			}
			CloseUtil.closeSocket(client);
		}
	
	}
	
	public void addClient(ClientModel client) {
		server.addClient(client);
	}
	public boolean addShareFile(ShareFileModel shareFile) {
		return server.addShareFile(shareFile);
	}
	
}
